package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /*
    * 添加购物车
    * */
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){

        //1、判断购物车是否存在改菜品或者套餐
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() !=null,"setmeal_id",shoppingCart.getSetmealId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart == null){
            //2.1 添加操作，往shopping_cart 表中添加一条数据
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);

        }else {
            //2.2 更新操作，将number++
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /*
    * 查询购物车
    * */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }



    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /*
    * 删除商品
    * */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        //1、判断购物车是否存在改菜品或者套餐
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() !=null,"setmeal_id",shoppingCart.getSetmealId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart.getNumber() <= 1){
            //如果大于等于1时，还进行减的话 就删除这条数据
            shoppingCartService.remove(queryWrapper);

        }else {
            //2.2 更新操作，将number++
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");



    }

}
